import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'

const orderStore = defineStore('order', () => {
  const cartItems = ref([])
  const cartConfig = reactive({
    dining_method: null,
    remark: '',
    order_id: null
  })

  const setCart = (items: any) => {
    cartItems.value = items
  }

  const setConfig = (config: any) => {
    cartConfig.dining_method = config.dining_method || null
    cartConfig.remark = config.remark || ''
    cartConfig.order_id = config.order_id || null
  }

  const clearCart = () => {
    cartItems.value = []
    cartConfig.dining_method = null
    cartConfig.remark = ''
    cartConfig.order_id = null
    localStorage.removeItem('cart_items')
  }

  return {
    cartItems,
    setCart,
    clearCart,
    cartConfig,
    setConfig
  }
}, {
  persist: {
    storage: localStorage,
    paths: ['cartItems'],
    key: 'cart_items',
  } as any
})

export default orderStore
